package com.promis.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.TimePicker;

import java.util.ArrayList;
import java.util.List;

public class ScreenUtils {

	public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
	}
	
	public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
	
	public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }
	
	public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

	@SuppressLint("NewApi")
	public static Rect getScreenRect(Context context) {
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		Rect rect = new Rect();
		wm.getDefaultDisplay().getRectSize(rect);
		return rect;
	}

	public static Rect getApplicationRect(Activity activity) {
		Rect rect = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
		return rect;
	}

	public static int getStatusBarHeight(Context context) {
		DisplayMetrics metrics = context.getResources().getDisplayMetrics();
		return (int) Math.ceil(24 * metrics.density);
	}
	
	/**
     * scrollview与listview合用会出现listview只显示一行多点。此方法是为了定死listview的高度就不会出现以上状况
     * 算出listview的高度
     */
    public static void resizeListViewHeight(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)) + listView.getPaddingTop() + listView.getPaddingBottom();
        listView.setLayoutParams(params);
    }
    
    public static void resizeDateTimePicker(Context context, DatePicker datePicker, TimePicker timePicker){
    	Rect rect = getScreenRect(context);
    	int width = rect.width();
    	int margin = 10;
    	width = (width - 40 - 10 * 5) / 5;
    	
    	List<NumberPicker> npList = findNumberPicker(datePicker);  
        for(NumberPicker np : npList){  
        	resizeNumberPicker(np, width, 0, margin);
        }
        
        npList = findNumberPicker(timePicker);
        for(NumberPicker np : npList){  
        	resizeNumberPicker(np, width, margin, 0);
        } 
    }
    
    @SuppressLint("NewApi")
	private static void resizeNumberPicker(NumberPicker np, int width, int leftMargin, int rightMargin){
    	LayoutParams params = new LayoutParams(width, LayoutParams.WRAP_CONTENT);
        params.setMargins(leftMargin, 0, rightMargin, 0);  
        np.setLayoutParams(params);
    }
    
    private static List<NumberPicker> findNumberPicker(ViewGroup viewGroup){  
        List<NumberPicker> npList = new ArrayList<NumberPicker>();  
        View child = null;  
        if(null != viewGroup){  
            for(int i = 0;i<viewGroup.getChildCount();i++){  
                child = viewGroup.getChildAt(i);  
                if(child instanceof NumberPicker){  
                    npList.add((NumberPicker)child);  
                }  
                else if(child instanceof LinearLayout){  
                    List<NumberPicker> result = findNumberPicker((ViewGroup)child);  
                    if(result.size()>0){  
                        return result;  
                    }  
                }  
            }  
        }  
        return npList;  
    }

    public static int getScreenWidth(Context context)
    {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE );
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics( outMetrics);
        return outMetrics .widthPixels ;
    }
}
